/*
* $Id: ContentSettings.cpp 2 2009-12-23 13:58:02Z volok-aleksej@yandex.ru $
*
* Copyright 2008 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "../UserMessage.h"
#include "Settings/InterfaceStn.h"
#include "DinamicContent/InterfaceDC.h"
#include "ContentSettings.h"
#include "SettingsWin.h"
#include "DinamicContent/Factories.h"

#define BEGIN_CONTROL_ID 0x2012

CContentSettings::CContentSettings(CSettingsWin* pParent){
	m_pParent = pParent;
}
CContentSettings::~CContentSettings(){
	DestroyDinamicInterface();
}

void CContentSettings::CreateDinamicInterface(const std::tstring& strNameSettings){
	DestroyDinamicInterface();
	UpdateWindow();
	m_strCurSettingName = strNameSettings;
	ISetting* pSetings = IApplication::GetApp()->GetSettingsManager()->GetSetting(strNameSettings);
	std::vector<sWindow*> aWindows = pSetings->GetDynamicInterface();
	int nId = BEGIN_CONTROL_ID;
	for(std::vector<sWindow*>::iterator it = aWindows.begin(); it != aWindows.end(); it++, nId += 0x0010)
	{
		m_aWindows.push_back(CControlFactory::CreateControl(*it, this, nId));
		delete *it;
	}
}
void CContentSettings::DestroyDinamicInterface(){
	for(std::vector<IControl*>::iterator it = m_aWindows.begin(); it != m_aWindows.end(); it++)
		dynamic_cast<IWnd*>(*it)->Free();
	m_aWindows.clear();
}
bool CContentSettings::IsValidate(){
	for(std::vector<IControl*>::iterator it = m_aWindows.begin(); it != m_aWindows.end(); it++){
		if(!(*it)->Validate()){
			return FALSE;}
	}
	return TRUE;
}

BEGIN_MESSAGE_MAP(CContentSettings, CWnd)
	ON_WM_PAINT()
	ON_MESSAGE(UM_DEPENDENT, OnDependent)
	ON_MESSAGE(UM_SETSTATE, OnSetState)
	ON_MESSAGE(UM_TABSTOP, OnTabStop)
END_MESSAGE_MAP()

LRESULT CContentSettings::OnSetState(WPARAM wParam, LPARAM lParam){
	IApplication::GetApp()->GetSettingsManager()->GetSetting(m_strCurSettingName)->SetState((TCHAR*)wParam, (TCHAR*)lParam);
	if(IsValidate())
		m_pParent->ApplyButtonState();
	return TRUE;
}
LRESULT CContentSettings::OnTabStop(WPARAM wParam, LPARAM lParam){
	bool bFind = false;
	for (std::vector<IControl*>::iterator it = m_aWindows.begin(); it != m_aWindows.end(); it++){
		if(bFind && (*it)->IsTabStop()){
			(*it)->TabStop();
			return TRUE;
		}
		if(*it == dynamic_cast<IControl*>((CCommonControl*)lParam))
			bFind = TRUE;
	}
	if((void*)lParam == GetParent() && !m_aWindows.empty())
		(*m_aWindows.begin())->TabStop();
	else
		GetParent()->SendMessage(UM_TABSTOP, 0, (LPARAM)this);
	return TRUE;
}
LRESULT CContentSettings::OnDependent(WPARAM wParam, LPARAM lParam){
	std::vector<IControl*>::iterator it;
	for (it = m_aWindows.begin(); it != m_aWindows.end(); it++){
		if((*it)->GetId() == (TCHAR*)lParam)
			break;
	}
	std::tstring strState = IApplication::GetApp()->GetSettingsManager()->GetSetting(m_strCurSettingName)->GetState((TCHAR*)lParam);
	(*it)->SetState(strState);
	return TRUE;
}
void CContentSettings::OnPaint(){
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(rect);
	CBrush brush;
	brush.CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
	dc.FillRect(rect, &brush);
}
void CContentSettings::UpdateStates(){
	for (std::vector<IControl*>::iterator it = m_aWindows.begin(); it != m_aWindows.end(); it++)	{
		std::tstring strState = IApplication::GetApp()->GetSettingsManager()->GetSetting(m_strCurSettingName)->GetCurrentValue((*it)->GetId());
		(*it)->SetState(strState);
	}
}